package com.caishi.lkx.exam.model;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zzw.common.entity.impl.BSDEntity;
import com.caishi.lkx.exam.QuestionCollection;
import com.caishi.lkx.exam.QuestionCollectionBlockItem;
import com.caishi.lkx.exam.ienum.type.PaperRecordType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * id=paperId时表示练习模式
 */
@Schema(title = "试卷记录")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "ex_paper_records", autoResultMap = true)
public class PaperRecordsModel extends BSDEntity<PaperRecordsModel> implements QuestionCollection<PaperRecordsModel, String> {

    @Schema(title = "试卷id")
    private String paperId;

    @Schema(title = "试卷类型")
    private PaperRecordType type;

    @Schema(title = "用户id")
    private String userId;

    @Schema(title = "记录名称")
    private String name;

    @Schema(title = "科目id")
    private Long categoryId;

    @Schema(title = "考试id")
    private String examId;

    @Schema(title = "做题时间")
    private Long seconds;

    @Schema(title = "是否通过")
    private Boolean hp;

    @Schema(title = "得分数")
    private Float score;

    @Schema(title = "总分数")
    private Float allScore;

    @Schema(title = "通过分数")
    private Float passScore;

    @Schema(title = "试卷错题数")
    private Integer qcount;

    @Schema(title = "做题数目")
    private Integer doCount;

    @Schema(title = "试卷错题数")
    private Integer errorCount;

    @Schema(title = "开始时间")
    private LocalDateTime startTime;

    @Schema(title = "结束时间")
    private LocalDateTime endTime;

    @Schema(title = "试卷完成次数")
    private Integer completeCount;

    @TableField(value = "content", typeHandler = JacksonTypeHandler.class)
    @Schema(title = "试卷内容")
    @JsonIgnore
    private List<Map<String, Object>> contentMap;


    @Schema(title = "考试时长")
    @TableField(exist = false)
    private Integer examTime;
    @TableField(exist = false)
    private String categoryName;
    @TableField(exist = false)
    private String examName;
    @TableField(exist = false)
    private String industryId;
    @TableField(exist = false)
    private String industryName;
    @TableField(exist = false)
    private String remark;

    public List<QuestionCollectionBlockItem> getContent() {
        if (contentMap != null) {
            return contentMap.stream().map(v -> BeanUtil.fillBeanWithMap(v, new QuestionCollectionBlockItem(), true)).toList();
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public void setContent(List<QuestionCollectionBlockItem> content) {
        if (content == null) {
            this.contentMap = null;
            return;
        }
        this.contentMap = content.stream().map(v -> JSON.parseObject(JSON.toJSONString(v)).getInnerMap()).toList();
    }

    public boolean getHaveFinish() {
        return endTime != null;
    }

}
